package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list", name = "查看附件列表")
    public String findAll(String contractId, String contractProductId, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(example, page, pageSize);
        request.setAttribute("page", pageInfo);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        request.setAttribute("contractProductId", contractProductId);
        request.setAttribute("contractId", contractId);
        return "cargo/extc/extc-list";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到修改附件页面")
    public String toUpdate(String id, String contractId, String contractProductId) {
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);
        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId", contractProductId);

        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "cargo/extc/extc-update";
    }

    @RequestMapping(value = "/edit", name = "保存附件信息")
    public String save(ExtCproduct extCproduct, String contractId, String contractProductId, MultipartFile productPhoto) {
        String upload = null;
        try {
            if (productPhoto != null) {
                upload = fileUploadUtil.upload(productPhoto);
            }
        } catch (Exception e) {
        }
        extCproduct.setProductImage(upload);

        if (StringUtils.isEmpty(extCproduct.getId())) {
            Factory factory = factoryService.findById(extCproduct.getFactoryId());
            extCproduct.setFactoryName(factory.getFactoryName());
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getUser().getCompanyId());
            extCproduct.setCompanyName(getUser().getCompanyName());
            extCproductService.save(extCproduct);
        } else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId + "&contractProductId=" + contractProductId;
    }

    @RequestMapping(value = "/delete", name = "删除附件")
    public String delete(String id, String contractId, String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" + contractId + "&contractProductId=" + contractProductId;
    }
}
